class News < ActiveRecord::Base
  mount_uploader :home_page_image, PhotoUploader

  CHECK = {'未送审' => 0,'已送审' => 1,'已采用' => 2,'未采用' => 3}

	paginates_per 20

	#default_scope ->{ self.order("created_at desc") }
  scope :check,->(check){check.blank? ? nil : where(:check => check.to_i)}
  scope :writer,->(writer){writer.blank? ? nil : where(:writer => writer)}
  scope :submited_news,->{where(:check => [1,2,3])}
  scope :category,->(cate_id){cate_id.blank? ? nil : where(:category_id => cate_id)}
  scope :news_id,->(news_id){news_id.blank? ? nil : where(:id => news_id.to_i)}
  scope :sptitle,->(sptitle){sptitle.blank? ? nil : where("sptitles.name" => sptitle)}
  scope :pubtime,->(begin_date,end_date){(begin_date.blank? && end_date.blank?) ? nil : where(:pubtime => begin_date.to_date.beginning_of_day..end_date.to_date.at_end_of_day) }
  scope :contribution_units_id,->(contribution_units_id){contribution_units_id.blank? ? nil : where(:contribution_units_id => contribution_units_id)}

	belongs_to :category
	belongs_to :department, :foreign_key => "contribution_units_id"
  #has_many :news_platforms
  #has_many :platforms,:through => :news_platforms
  has_many :news_groups
  has_many :groups,:through => :news_groups
  has_many :news_sptitles
  has_many :sptitles,:through => :news_sptitles

  validates_presence_of :category_id,:title,:writer,:body,:contribution_units,:category_id
  validate :check_author_for_pic

  def check_text
    #CHECK.map{|k,v| [v,k]}.to_h[check]
    case check
      when 0
        '未送审'
      when 1
        '已送审'
      when 2
        '已采用'
      when 3
        '未采用'
    end
  end

  #首页显示的news
  def self.index_news
    admin_group = Group.where(:name => '管理员').first
    News.joins(:news_groups).where("news_groups.group_id" => admin_group.id,"news_groups.check" => 1)
  end

  #新闻类型（我的稿件，平台稿件）
  def self.news_type news_type,user
    case news_type
      when "1"
        self.where(:postby => user.name)
      when "2"
        if user.admin?
          self.submited_news
        else
          self.joins(:groups).where("groups.id" => user.groups.collect(&:id)).submited_news
        end
    end
  end

  #是否能编辑和删除某条新闻
  def self.can_edit? news,user
    #if news.check == 0 || !(news.groups & user.groups).blank? || user.admin?
    if news.check == 0 || user.admin?
      true
    else
      false
    end
  end

  #某个用户是否有审核某条新闻的权限
  def self.can_approve? news,user

    group = user.groups.first
    group_news = NewsGroup.where(:news_id => news.id,:group_id => group.id).first
    if news.check != 0 && (news.groups & user.groups).present? && (group_news.check.blank? || group_news.check == 2)
      if group.name != '管理员' && group_news.check.present?
        false
      else
        true
      end

    else
      false
    end
  end

  def get_categories_news categories
      SHENDUBAODAO_CATEGORIES
  end

  #某条新闻是否已经被新闻网采用
  def admin_approved?
    admin_group = Group.where(:name => '管理员').first
    news = NewsGroup.where(:news_id => self.id,:group_id => admin_group.id).first
    if !news
      false
    elsif news.check == 1
      true
    else
      false
    end
  end

  protected
  #如果有照片,检查是否填入了摄影师
  def check_author_for_pic
    result = false
    doc = Hpricot(body)
    if doc
      first_image = (doc/"img").first
      if first_image
        result = true
      end
    end
    if result && cameraman_name.blank?
      errors.add(:cameraman_name,'不能为空')
    end
  end

end
